package com.lyh.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lyh.mall.product.entity.ProductAttrValueEntity;
import com.lyh.mall.product.service.AttrService;
import com.lyh.mall.product.service.ProductAttrValueService;
import com.lyh.mall.product.vo.AttrRespVo;
import com.lyh.mall.product.vo.AttrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyh.common.utils.PageUtils;
import com.lyh.common.utils.R;



/**
 * 商品属性
 *
 * @author lyh
 * @email 1964059406@qq.com
 * @date 2021-09-25 21:18:00
 */
@Api("商品属性")
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     *  /product/attr/update/{spuId}
     */
    @ApiOperation(value = "修改商品规格")
    @PostMapping("/update/{spuId}")
    public R upduteSpuAttr(@PathVariable("spuId")Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entityList){
        productAttrValueService.upduteSpuAttr(spuId,entityList);
        return R.ok();
    }

    /**
     * /product/attr/base/listforspu/{spuId}
     */
    @ApiOperation(value = "获取spu规格")
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entityList = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data",entityList);
    }

    /**
     * 获取分类规格参数: /product/attr/base/list/{catelogId}
     * 获取分类销售属性: /product/attr/sale/list/{catelogId}
     * type 属性类型[0-销售属性，1-基本属性]
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    @ApiOperation(value = "获取分类(规格参数)或者(销售属性)",
            notes = "获取分类规格参数——/product/attr/base/list/{catelogId}" +
                    "获取分类销售属性——/product/attr/sale/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查询属性详情
     */
    @ApiOperation(value = "查询属性详情")
    @GetMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo respVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", respVo);
    }

    /**
     * 保存属性【规格参数，销售属性】
     */
    @ApiOperation(value = "保存属性【规格参数，销售属性】")
    @PostMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改属性
     */
    @ApiOperation(value = "修改属性")
    @PostMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
		attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
